<?php
	# SEMS Lite shared library

### DATABASE ###

	# Connect to the group database
	#function connect(){
	#	$connection = mysql_connect("clipper.encs.concordia.ca", "myc353_2", "jKMAQF") or
	#		die("Cannot connect to database");
	#	mysql_select_db('myc353_2');
	#	return $connection;
	#}

	function connect(){
		$connection = mysql_connect("localhost", "myc353_2", "jKMAQF") or
			die("Cannot connect to database");
		mysql_select_db('myc353_2');
		return $connection;
	}

	# Connect to the read-only database
	function connectToExternalDB(){
		$connection = mysql_connect("clipper.encs.concordia.ca", "cs353_2", "letmein") or
			die("Cannot connect to database");
		mysql_select_db('cs353db');
		return $connection;
	}

### CONFIGURATION ###
	date_default_timezone_set("America/Montreal");
	#session_save_path("./sessions");
	session_start();

### USER VALIDATION ###

	# Administrators
	$GLOBALS['admins'] = array("d_sinea","e_bozi","jh_taylo","r_conigl");

	# Test if the user is an administrator
	function isAdmin ($username) {
		$admins = $GLOBALS['admins'];
		foreach($admins as $name){
			if($name === $username)
				return true;
		}
		return false;
	}

	# Test if the user is the program chair of the event
	function isProgramChair ($userid, $eventid=null){
		$cn = connect();
		if($eventid !== null)
			$r  = mysql_query("SELECT role FROM subscribers
				WHERE users_idusers = \"$userid\" AND events_idevents = \"$eventid\" AND role = \"Program_chair\"");
		else
			$r  = mysql_query("SELECT role FROM subscribers
				WHERE users_idusers = \"$userid\" AND role = \"Program_chair\"");
		mysql_close($cn);
		if(mysql_num_rows($r) == 0 )
			return false;
		return true;
	}

	# Test if the user is a comitte member of the event
	function isCommittee ($userid, $eventid=null){
		$cn = connect();
		if($eventid !== null)
			$r  = mysql_query("SELECT role FROM subscribers
				WHERE users_idusers = \"$userid\" AND events_idevents = \"$eventid\" AND role = \"Comittee\"");
		else
			$r  = mysql_query("SELECT role FROM subscribers
				WHERE users_idusers = \"$userid\" AND role = \"Comittee\"");
		mysql_close($cn);
		if(mysql_num_rows($r) == 0 )
			return false;
		return true;
	}

	# Test if the user participate to the event
	function isParticipant ($userid, $eventid){
		$cn = connect();
		$r  = mysql_query("SELECT role FROM subscribers
			WHERE users_idusers = \"$userid\" AND events_idevents = \"$eventid\" AND role = \"Participant\"");
		mysql_close($cn);
		if(mysql_num_rows($r) == 0 )
			return false;
		return true;
	}

	# Test if the user is an anonymous visitator or new commer
	function isVisitor(){
		if(! isset($_SESSION['idusers']))
			return true;
		return false;
	}

	function getMilestone($eventid){
		$cn = connect();
		$r = mysql_query("SELECT dl_submissions, dl_bidding, dl_ratings, end FROM events WHERE idevent='".$eventid."'");
		$row = mysql_fetch_assoc($r);
		$now = date_create(); 
		$k = 0;
		foreach($row as $name => $datestr){
			$date = date_create($datestr);
			if($date == null or $now < $date)
				break;
			else
				$k++;
		}
		mysql_close($cn);
		return $k;
	}

?>
